﻿Imports QLPhuTungOto.Data
Imports QLPhuTungOto.Bus
Imports DevExpress.XtraGrid.Controls

Public Class ucQLKhachHang

    Dim KH_BUS As New KhachHangBUS
    Dim lst_kh As New List(Of KhachHang)

    Dim TrangHienTai As Integer = 1
    Dim TongSoTrang As Integer = 1
    Dim SoDongTrenTrang As Integer = 0


    Private Sub ucQLKhachHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            UcPhanTrang1.txtSoDongCuaTrang.Text = HamHoTro.DocThongSo("config.config", "PhanTrangKhachHang", "200")
            SoDongTrenTrang = Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text)

            TinhSoTrang()
            LoadDSKhachHang()

            Dim f As Form = Me.FindForm()
            If f Is Nothing Then
                Return
            End If
            f.KeyPreview = True
            AddHandler f.KeyDown, AddressOf Form_KeyDown

        Catch ex As Exception

        End Try

    End Sub

    Private Sub LoadDSKhachHang2()
        gcDSKhachHang.DataSource = KH_BUS.LayDS()
    End Sub
    Private Sub LoadDSKhachHang()
        SoDongTrenTrang = Integer.Parse(Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text))
        lst_kh = KH_BUS.LayDS((TrangHienTai - 1) * SoDongTrenTrang, SoDongTrenTrang)
        gcDSKhachHang.DataSource = lst_kh
        UcPhanTrang1.lblTrangHienTai.Text = TrangHienTai.ToString + "/" + TongSoTrang.ToString
    End Sub

    Private Sub gvDSKhachHang_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles gvDSKhachHang.FocusedRowChanged
        'tên, sdt, fax, diachi, thongtinkhac
        Dim selectedindex As Integer = 0
        Try
            selectedindex = gvDSKhachHang.FocusedRowHandle
            txtTenKhachHang.Text = gvDSKhachHang.GetRowCellValue(selectedindex, "TenKhachHang")
            txtSDT.Text = gvDSKhachHang.GetRowCellValue(selectedindex, "Sdt")
            txtFax.Text = gvDSKhachHang.GetRowCellValue(selectedindex, "Fax")
            txtDiaChi.Text = gvDSKhachHang.GetRowCellValue(selectedindex, "DiaChi")
            txtThongTinKhac.Text = gvDSKhachHang.GetRowCellValue(selectedindex, "ThongTinKhac")

            LayDSSanPhamDaMua()
        Catch ex As Exception

        End Try
        
    End Sub

    Private Sub LayDSSanPhamDaMua()
        gcSanPhamDaMua.DataSource = KH_BUS.LayDSSanPhamDaMua(Integer.Parse(gvDSKhachHang.GetRowCellValue(gvDSKhachHang.FocusedRowHandle, "MaKhachHang")))
    End Sub

    Private Function KiemTraNhap() As String
        Dim err As String = ""
        err += HamHoTro.KiemTraRong(txtTenKhachHang.Text, "Tên khách hàng")
        Return err
    End Function

    Private Function KiemTraTrung() As String
        Dim err As String = ""
        Dim kh As New KhachHang
        KH_BUS = New KhachHangBUS
        kh = KH_BUS.LayKhachHangTheoTen(txtTenKhachHang.Text)
        If kh.TenKhachHang = txtTenKhachHang.Text Then
            err += "Tên khách hàng này đã tồn tại, bạn có muốn tiếp tục không?"
        End If

        Return err
    End Function

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Try
            Dim err As String = ""
            err = KiemTraNhap()

            If err <> "" Then
                MessageBox.Show(err, "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Else
                Dim trung As String = ""
                trung = KiemTraTrung()
                If trung <> "" Then
                    If MessageBox.Show(trung, "Câu hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                        them()
                        TrangThai.ThayDoiKhachHang()
                        LoadDSKhachHang()
                    End If
                Else
                    them()
                    TrangThai.ThayDoiKhachHang()
                    LoadDSKhachHang()
                End If
            End If

        Catch ex As Exception
            MessageBox.Show("Không thể thêm khách hàng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        
    End Sub

    Private Sub them()
        Dim kh As New KhachHang With {.TenKhachHang = txtTenKhachHang.Text,
                                      .DiaChi = txtDiaChi.Text,
                                      .Sdt = txtSDT.Text,
                                      .Fax = txtFax.Text,
                                      .ThongTinKhac = txtThongTinKhac.Text}
        KH_BUS.Them(kh)
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Try
            If MessageBox.Show("Bạn có chắc muốn xóa khách hàng """ + gvDSKhachHang.GetRowCellValue(gvDSKhachHang.FocusedRowHandle, "TenKhachHang").ToString() _
                               + """ ?", "Câu hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then

                If gvDSKhachHang.GetRowCellValue(gvDSKhachHang.FocusedRowHandle, "MaKhachHang").ToString() = "1" Then
                    MessageBox.Show("Đây là khách hàng mặc định, bạn không thể xóa.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                Else
                    Xoa()
                    TrangThai.ThayDoiKhachHang()
                    LoadDSKhachHang()
                End If
            End If

        Catch ex As Exception
            MessageBox.Show("Không thể xóa khách hàng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        
    End Sub

    Private Sub Xoa()
        Dim maKH As Integer
        maKH = Integer.Parse(gvDSKhachHang.GetRowCellValue(gvDSKhachHang.FocusedRowHandle, "MaKhachHang").ToString())
        KH_BUS.Xoa(maKH)
    End Sub

    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapLai.Click

        Try
            txtTenKhachHang.Text = ""
            txtDiaChi.Text = ""
            txtSDT.Text = ""
            txtFax.Text = ""
            txtThongTinKhac.Text = ""
        Catch ex As Exception

        End Try
        
    End Sub

    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        Try
            If gvDSKhachHang.GetRowCellValue(gvDSKhachHang.FocusedRowHandle, "MaKhachHang").ToString() = "1" Then
                MessageBox.Show("Đây là khách hàng mặc định, bạn không thể thay đổi thông tin.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Else
                Dim err As String = ""
                err = KiemTraNhap()

                If err = "" Then
                    Sua()
                    TrangThai.ThayDoiKhachHang()
                    LoadDSKhachHang()
                Else
                    MessageBox.Show(err, "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("Không thể cập nhật thông tin khách hàng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        
    End Sub

    Private Sub Sua()
        Dim kh As New KhachHang With {.MaKhachHang = Integer.Parse(gvDSKhachHang.GetRowCellValue(gvDSKhachHang.FocusedRowHandle, "MaKhachHang")),
                                       .TenKhachHang = txtTenKhachHang.Text,
                                       .DiaChi = txtDiaChi.Text,
                                       .Sdt = txtSDT.Text,
                                       .Fax = txtFax.Text,
                                       .ThongTinKhac = txtThongTinKhac.Text}
        KH_BUS.Sua(kh)
    End Sub

    Private Sub CapNhatStatus()
        ToolStripStatusLabel1.Text = "Số lượng khách hàng: " + KH_BUS.DemSoLuongKhachHang().ToString
    End Sub

    Private Sub gcDSKhachHang_DataSourceChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gcDSKhachHang.DataSourceChanged
        CapNhatStatus()
    End Sub

    Private Sub ucQLKhachHang_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        
    End Sub

    Private Sub UcPhanTrang1_SuaSoDongTrenTrang() Handles UcPhanTrang1.SuaSoDongTrenTrang
        Try
            SoDongTrenTrang = Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text)

            If SoDongTrenTrang > 0 Then
                TinhSoTrang()
                TrangHienTai = 1
                LoadDSKhachHang()
                HamHoTro.GhiThongSo("config.config", "PhanTrangKhachHang", UcPhanTrang1.txtSoDongCuaTrang.Text.Trim)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangCuoi_Click() Handles UcPhanTrang1.TrangCuoi_Click
        Try
            TrangHienTai = TongSoTrang
            LoadDSKhachHang()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangDau_Click() Handles UcPhanTrang1.TrangDau_Click
        Try
            TrangHienTai = 1
            LoadDSKhachHang()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangSau_Click() Handles UcPhanTrang1.TrangSau_Click
        Try
            If TrangHienTai < TongSoTrang Then
                TrangHienTai += 1
                LoadDSKhachHang()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangTruoc_Click() Handles UcPhanTrang1.TrangTruoc_Click
        Try
            If TrangHienTai > 1 Then
                TrangHienTai -= 1
                LoadDSKhachHang()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function TinhSoTrang() As Integer
        Dim st As Integer = 0
        Try

            Dim soluongkh As Integer = KH_BUS.DemSoLuongKhachHang
            st = (soluongkh \ SoDongTrenTrang)

            If soluongkh Mod SoDongTrenTrang <> 0 Then
                st += 1
            End If

            TongSoTrang = st

        Catch ex As Exception

        End Try
        Return st
    End Function

    Dim flag1 As Boolean = False
    Dim isDel As Boolean = False
    Private Sub Form_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        If TypeOf (Me.ActiveControl) Is FindControl Then
            Dim c As FindControl = Me.ActiveControl
            Dim s As String = c.Controls(0).Controls(5).Text
            If e.KeyCode = Keys.Back Then
                isDel = True
            End If

            If isDel = False Then
                If flag1 = False Then
                    LoadDSKhachHang2()
                    flag1 = True
                End If
            ElseIf isDel = True AndAlso s.Count = 1 Then
                LoadDSKhachHang()
                flag1 = False
                isDel = False
            End If
        End If



    End Sub

    Private Sub label1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles label1.Paint
        Try

            If TrangThai.ThayDoiKhachHang_KhachHang Then
                TinhSoTrang()
                LoadDSKhachHang()
                TrangThai.ThayDoiKhachHang_KhachHang = False
            End If

        Catch ex As Exception

        End Try
    End Sub
End Class
